<template>
  <el-form :model="form" ref="form" :rules="rules" class="form">
    <el-form-item class="form-item" prop="username">
      <el-input v-model="form.username" placeholder="用户名手机"></el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="captcha">
      <el-input v-model="form.captcha" placeholder="验证码">
        <template slot="append">
          <el-button @click="handleSendCaptcha">发送验证码</el-button>
        </template>
      </el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="nickname">
      <el-input v-model="form.nickname" placeholder="你的名字"></el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="password">
      <el-input v-model="form.password" placeholder="密码" type="password"></el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="password2">
      <el-input v-model="form.password2" placeholder="确认密码" type="password"></el-input>
    </el-form-item>

    <el-button class="submit" type="primary" @click="handleRegSubmit">注册</el-button>
  </el-form>
</template>

<script>
export default {
  data() {
       var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.form.password2 !== '') {
            this.$refs.form.validateField('password2');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.form.password) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
    return {
      // 表单数据
      form: {
        // 手机号码
        username: "13025839751",
        // 昵称
        nickname: "前进",
        // 手机验证码
        captcha: "000000",
        // 密码
        password: "123",
        // 确认密码
        password2: "123",
      },
      // 表单规则
      rules: {
        username: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          { message:"手机号码格式不对", pattern: /^1[3456789]\d{9}$/, trigger: "blur" }
        ],
        nickname: [{ required: true, message: "请输入昵称", trigger: "blur" }],
        captcha: [{ required: true, message: "请输入验证码", trigger: "blur" }],
        password: [
            { required: true, message: "请输入密码", trigger: "blur" },
            { validator: validatePass, trigger: 'blur' }
        ],
        password2: [
          { required: true, message: "请再输入密码", trigger: "blur" },
          { validator: validatePass2, trigger: 'blur' }
        ],
      },
    };
  },
  methods: {
    // 发送验证码
    handleSendCaptcha() {
      this.$refs.form.validateField("username", (msg) => {
        //    console.log(msg);
        if (msg === "") {
             console.log('通过');
          this.$axios
            .post("/captchas", { tel: this.form.username })
            .then((res) => {
              // console.log(res);
            });
        }else{
            console.log('不通过');
        }
      });
    },

    // 注册
    handleRegSubmit() {
      //   console.log(this.form);
        this.$refs.form.validate((valid) => {
          if (valid) {
             const{password2,...data} = this.form
             this.$axios.post('/accounts/register',data)
             .then(res=>{
                //  console.log(res);
                this.$message({
                    type:'success',
                    message:'注册成功'
                })
                // 跳转到登录页面
                 location.reload(); 
             })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
    },
  },
};
</script>

<style scoped lang="less">
.form {
  padding: 25px;
}

.form-item {
  margin-bottom: 20px;
}

.form-text {
  font-size: 12px;
  color: #409eff;
  text-align: right;
  line-height: 1;
}

.submit {
  width: 100%;
  margin-top: 10px;
}
</style>